package com.example.parkinglotmanagementsystem;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.scene.control.*;
import javafx.stage.Stage;

import static com.example.parkinglotmanagementsystem.Application.mainScene;
import static com.example.parkinglotmanagementsystem.MainController.EditID;
import static com.example.parkinglotmanagementsystem.MainController.data;

public class EditController {

    private enum Type{
        SedanCar,Truck,Bus
    }
    Type type = Type.SedanCar;

    public Button OKButton;
    public Button CancelButton;
    public TextField number;
    public TextField company;
    public TextField time;
    public TextField totalKilo;
    public TextField consumption;
    public TextField maintenanceExpense;
    public TextField roadMaintenanceExpense;
    public TextField uniqueAttributes;
    public ToggleButton ToggleSedanCar;
    public ToggleButton ToggleBus;
    public ToggleButton ToggleTruck;
    public ToggleGroup group = new ToggleGroup();
    public Label labelUniqueAttributes;

    public void initialize(){
        initToggleGroup();
        initText();
    }

    public void initToggleGroup(){
        ToggleBus.setToggleGroup(group);
        ToggleSedanCar.setToggleGroup(group);
        ToggleTruck.setToggleGroup(group);
        ToggleTruck.setUserData("载重量");
        ToggleBus.setUserData("载客量");
        ToggleSedanCar.setUserData("车厢数");

        group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observableValue, Toggle toggle, Toggle t1) {
                if(t1==null){
                    uniqueAttributes.setPromptText("车厢数");
                    labelUniqueAttributes.setText("车厢数");
                }
                else {
                    String s = (String) group.getSelectedToggle().getUserData();
                    labelUniqueAttributes.setText(s);
                    uniqueAttributes.setPromptText(s);
                    switch (s) {
                        case "载重量" -> type = Type.Truck;
                        case "载客量" -> type = Type.Bus;
                        case "车厢数" -> type = Type.SedanCar;
                    }
                }

            }
        });
    }

    public void initText(){
        Vehicle v = data.get(EditID-1);
        number.setText(v.getNumber());
        company.setText(v.getCompany());
        time.setText(v.getTime());
        totalKilo.setText(String.valueOf(v.getTotalKilo()));
        consumption.setText(String.valueOf(v.getConsumption()));
        maintenanceExpense.setText(String.valueOf(v.getMaintenanceExpense()));
        roadMaintenanceExpense.setText(String.valueOf(v.getRoadMaintenanceExpense()));
        uniqueAttributes.setText(v.getUniqueAttributes());
        switch (v.getType()){
            case "小轿车" -> {
                type = Type.SedanCar;
                ToggleSedanCar.setSelected(true);
            }
            case "卡车" -> {
                type = Type.Truck;
                ToggleTruck.setSelected(true);
            }
            case "大巴车" -> {
                type = Type.Bus;
                ToggleBus.setSelected(true);
            }
        }
    }

    public void OnOKAction() {
        String number1 = number.getText();
        String company1 = company.getText();
        String time1 = time.getText();
        float totalKilo1 = Float.parseFloat(totalKilo.getText());
        float consumption1 = Float.parseFloat(consumption.getText());
        float expense1 = Float.parseFloat(maintenanceExpense.getText());
        float roadExpense1 = Float.parseFloat(roadMaintenanceExpense.getText());
        float totalExpense1 = expense1 + roadExpense1;

        switch (type) {
            case Truck -> {
                float loadedWeight1 = Float.parseFloat(uniqueAttributes.getText());
                Truck truck = new Truck(EditID, number1, company1, time1, "卡车",
                        totalKilo1, consumption1,
                        expense1, roadExpense1, totalExpense1
                        , loadedWeight1);
                data.set(EditID - 1, truck);
            }
            case SedanCar -> {
                int carriageNumber1 = Integer.parseInt(uniqueAttributes.getText());
                SedanCar sedanCar = new SedanCar(EditID, number1, company1, time1, "小轿车",
                        totalKilo1, consumption1,
                        expense1, roadExpense1, totalExpense1
                        , carriageNumber1);
                data.set(EditID - 1, sedanCar);
            }
            case Bus -> {
                float cargoCapacity1 = Float.parseFloat(uniqueAttributes.getText());
                Bus bus = new Bus(EditID, number1, company1, time1, "大巴车",
                        totalKilo1, consumption1,
                        expense1, roadExpense1, totalExpense1
                        , cargoCapacity1);
                data.set(EditID - 1, bus);
            }
        }

        Stage stage = (Stage) CancelButton.getScene().getWindow();
        stage.setScene(mainScene);
    }

    public void OnCancelAction() {
        Stage stage = (Stage) CancelButton.getScene().getWindow();
        stage.setScene(mainScene);
    }
}
